<template>
  <div>
    <el-dialog
      title="提交审核"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm()"
      :close-on-click-modal="false"
    >
      <el-row :gutter="20">
        <el-col :span="24" v-if="tisp[0].isShow">
          <el-checkbox v-model="flow01">
            {{ tisp[0].name }}
          </el-checkbox>
        </el-col>
        <el-col :span="24" v-if="tisp[1].isShow">
          <el-checkbox v-model="flow02">
            {{ tisp[1].name }}
          </el-checkbox>
        </el-col>
        <el-col :span="24" v-if="tisp[2].isShow">
          <el-checkbox v-model="flow03">
            {{ tisp[2].name }}
          </el-checkbox>
        </el-col>
        <el-col :span="4">
          <span>上传审查表</span>
        </el-col>
        <el-col :span="20">
          <UploadFile v-if="dialogVisible" multiple @e-fileData="saveFile" />
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm()"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sysKnowledgeQuery } from '@/api/admin/courseLibrary/sysKnowledge.js'
import { flowApiSubmit } from '@/api/admin/audit/flowApi.js'
import { sysCourseUpdate } from '@/api/admin/courseLibrary/sysCourse.js'
import { sysCourseFilesAdd } from '@/api/admin/courseLibrary/sysCourseFiles.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    UploadFile: () => import('@/components/uploadFile/index.vue'),
  },
  data() {
    return {
      homeConfig: [],
      dialogVisible: false,
      flow01: false,
      flow02: false,
      flow03: false,
      checkedData: {},
      listQuery: {
        page: 1,
        limit: 2,
        courseId: '',
      },
      form: {
        fileId: '', //资源ID
        courseId: '',
        type: '11',
        des: '审查表', //资源介绍
      },
      tisp: [
        { id: 1, isShow: true, name: '已通过保密审查（资源为秘密级以下）' },
        { id: 2, isShow: true, name: '已通过政治审查' },
        { id: 3, isShow: true, name: '已完成线下保密、政治审查流程' },
      ],
    }
  },
  mounted() {
    Bus.$on('showDialogCourseAudit', (v) => {
      this.checkedData = v
      this.checkCourse()
      this.showAuditTips()
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogCourseAudit')
  },
  methods: {
    //获取知识点数据，判断是否符合发布标准
    checkCourse() {
      this.listQuery.courseId = this.checkedData.id
      sysKnowledgeQuery(this.listQuery).then((res) => {
        let { count } = res.data
        let msg =
          this.checkedData.courseType == 1
            ? '请完善章节、知识点信息'
            : '请进入管理后，在资源管理中添加资源。'
        if (count == 0) {
          this.$message({
            message: msg,
            type: 'warning',
          })
          return
        }
        this.dialogVisible = true
      })
    },
    showAuditTips() {
      this.homeConfig = JSON.parse(this.getSession('homeConfig')) || []
      let obj1 = this.homeConfig.find((v) => v.code == 'autip01')
      let obj2 = this.homeConfig.find((v) => v.code == 'autip02')
      let obj3 = this.homeConfig.find((v) => v.code == 'autip03')
      if (obj1) {
        this.tisp[0].isShow = obj1.isShow == 0
        this.tisp[0].name = obj1.name
      }
      if (obj2) {
        this.tisp[1].isShow = obj2.isShow == 0
        this.tisp[1].name = obj2.name
      }
      if (obj3) {
        this.tisp[2].isShow = obj3.isShow == 0
        this.tisp[2].name = obj3.name
      }
    },
    //显示弹窗
    submitForm() {
      if (this.flow01 && this.flow02 && this.flow03) {
        this.flowSubmit()
      } else {
        this.$message({
          message: '请勾选审查选项',
          type: 'warning',
        })
      }
    },
    //审核流程
    flowSubmit() {
      this.filesAdd()
      // return
      let { schoolDeptId } = this.$store.getters
      let { id, courseType } = this.checkedData
      if (schoolDeptId) {
        flowApiSubmit({
          businessKey: id,
          definitionCode: courseType,
          scope: schoolDeptId,
        }).then((res) => {
          if (!res || res.data.code != 0) return
          Bus.$emit('refreshCourseList')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.dialogVisible = false
        })
      } else {
        sysCourseUpdate({ id, auditStatus: 2, status: 1 }).then((res) => {
          if (!res || res.data.code != 0) return
          Bus.$emit('refreshCourseList')
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.dialogVisible = false
        })
      }
    },
    saveFile(v) {
      this.form.fileId = v.map((item) => item.id).toString()
    },
    //新增
    filesAdd() {
      if (!this.form.fileId) return
      this.form.courseId = this.checkedData.id
      sysCourseFilesAdd(this.form).then((res) => {})
    },
    //重置表单
    resetForm() {
      this.flow01 = false
      this.flow02 = false
      this.flow03 = false
    },
  },
}
</script>